
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;


namespace Swaf.Call
{
	/// <summary>
	/// Summary description for WebServiceCall.
	/// </summary>
	public class SimpleWebServiceRuleMgrCall : BaseCall
	{
		protected string m_serviceURL;
		protected string m_baseDomain;

		private static string s_callFormat = "rulename={0}&param={1}&paramType={2}";

		public SimpleWebServiceRuleMgrCall(string name, XmlElement initInfo)
			:base(name, initInfo)
		{
			IApplication app = Application.currentApp;
			m_baseDomain = app.resMgr.resolve(initInfo.GetAttribute("baseUrl"));
		}
		public SimpleWebServiceRuleMgrCall(IBizObj ruleDef) : base(ruleDef)
		{
			m_baseDomain = (string) ruleDef.get("extraInfo");
		}

		public override object call(params object[] p)
		{
			IApplication app = Application.currentApp;
			if(p.Length == 3 && p[0] is IApplication)
			{
				app = (IApplication)p[0];
				p = new object[]{p[2]};
			}

			//app.Log.println("SimpleWebServiceRuleMgrCall to {0}", m_baseDomain + m_className);
			HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m_baseDomain + m_className);
			req.Method = "POST";
			string sidn = (string)app.sessionData["sidn"];
			string sidv = (string)app.sessionData["sidv"];
			string stsn = (string)app.sessionData["stsn"];
			string stsv = (string)app.sessionData["stsv"];
			req.Headers.Add(String.Format("Cookie: {0}={1}; {2}={3}", sidn, sidv, stsn, stsv));
			req.ContentType = "application/x-www-form-urlencoded";
			
			SerializationInfo paramInfo	= Serialization.serialize(this.calcCallParams(p)[0]);
			string postContent = String.Format(s_callFormat, app.resMgr.fixupForHttp(this.Id), app.resMgr.fixupForHttp(paramInfo.SerialObject), paramInfo.TypeName);
			req.ContentLength = postContent.Length;
			Stream stmRequestStream=req.GetRequestStream();
			stmRequestStream.Write(Encoding.UTF8.GetBytes(postContent),0,postContent.Length);
			stmRequestStream.Close();
			WebResponse resp = req.GetResponse();
			Stream str = resp.GetResponseStream();
			string respOut = new StreamReader(str).ReadToEnd();
			str.Close();
			XmlDocument doc = new XmlDocument();
			doc.LoadXml(respOut);
			if(doc.DocumentElement.Name.Equals("RuleCallResults"))
				return BasicResults.createFromXml(doc.DocumentElement);
			return doc.DocumentElement;
		}

	}
}
